package com.lb.activity.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lb.activity.mapper.DictionaryDataMapper;
import com.lb.activity.mapper.DictionaryMapper;
import com.lb.activity.po.Dictionary;
import com.lb.activity.po.DictionaryData;
import com.lb.activity.po.DictionaryDataExample;
import com.lb.activity.po.DictionaryExample;
import com.lb.activity.sys.service.DictionaryService;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.util.SnowflakeIdWorker;
import com.lb.activity.util.emumdata.DataEnum;
import com.lb.activity.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author 001030
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {


    @Autowired
    private SnowflakeIdWorker idWorker;

    @Resource
    private DictionaryMapper dictionaryMapper;

    @Resource
    private DictionaryDataMapper dictionaryDataMapper;

    @Override
    public ResultUtil addDictionary(Dictionary dictionary) {
        String dictValue = dictionary.getDictValue();
        DictionaryExample example = new DictionaryExample();
        DictionaryExample.Criteria criteria = example.createCriteria();
        criteria.andDictNameEqualTo(dictValue);
        int i1 = dictionaryMapper.countByExample(example);
        if(i1 > 0){
            return ResultUtil.ok(false);
        }
        dictionary.setId(idWorker.nextId());
        dictionary.setStatus(DataEnum.INSERT_STATUS.getStatus());
        int i = dictionaryMapper.insertSelective(dictionary);
        return ResultUtil.ok(i>0);
    }

    /**
     * 列表
     * @param pageVo
     * @return
     */
    @Override
    public ResultUtil dictActs(PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        DictionaryExample example = new DictionaryExample();
        DictionaryExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(DataEnum.INSERT_STATUS.getStatus());
        List<Dictionary> dictionaries = dictionaryMapper.selectByExample(example);
        PageInfo<Dictionary> pageInfo = new PageInfo(dictionaries);
        return ResultUtil.build(0,"",Integer.parseInt(pageInfo.getTotal()+""),dictionaries);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @Override
    public Dictionary dictionaryById(Long id) {
        return dictionaryMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新
     * @param dictionary
     * @return
     */
    @Override
    public ResultUtil modifyDictionary(Dictionary dictionary) {
        return ResultUtil.ok(dictionaryMapper.updateByPrimaryKeySelective(dictionary)>0);
    }

    @Override
    public List<Dictionary> dictActs() {
        DictionaryExample example = new DictionaryExample();
        DictionaryExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(DataEnum.INSERT_STATUS.getStatus());
        List<Dictionary> dictionaries = dictionaryMapper.selectByExample(example);
        return dictionaries;
    }

    @Override
    public ResultUtil addDictionaryData(DictionaryData dictionaryData) {
        String dictDataValue = dictionaryData.getDictdataValue();
        DictionaryDataExample example = new DictionaryDataExample();
        DictionaryDataExample.Criteria criteria = example.createCriteria();
        criteria.andDictdataNameEqualTo(dictDataValue);
        int i1 = dictionaryDataMapper.countByExample(example);
        if(i1 > 0){
            return ResultUtil.ok(false);
        }
        dictionaryData.setId(idWorker.nextId());
        dictionaryData.setStatus(DataEnum.INSERT_STATUS.getStatus());
        //默认不固定
        dictionaryData.setIsfixed(DataEnum.DATA_NOT_FIXED.getStatus());
        int i = dictionaryDataMapper.insertSelective(dictionaryData);
        return ResultUtil.ok(i>0);
    }

    @Override
    public ResultUtil dictDatas(PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        DictionaryDataExample example = new DictionaryDataExample();
        DictionaryDataExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(DataEnum.INSERT_STATUS.getStatus());
        List<DictionaryData> dictionaries = dictionaryDataMapper.selectByExample(example);
        PageInfo<DictionaryData> pageInfo = new PageInfo(dictionaries);
        return ResultUtil.build(0,"",Integer.parseInt(pageInfo.getTotal()+""),dictionaries);
    }

    @Override
    public ResultUtil delDictData(Long id) {
        DictionaryData dictionaryData = DictionaryData.builder().id(id).status(DataEnum.DELETE_STATUS.getStatus()).build();
        int i = dictionaryDataMapper.updateByPrimaryKeySelective(dictionaryData);
        return ResultUtil.ok(i>0);
    }

    @Override
    public DictionaryData dictionaryDataById(Long id) {
        return dictionaryDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultUtil updateDictionaryData(DictionaryData dictionaryData) {
        int i = dictionaryDataMapper.updateByPrimaryKeySelective(dictionaryData);
        return ResultUtil.ok(i>0);
    }


    @Override
    public List<DictionaryData> dictionaryDatas(String dictValue) {
        DictionaryDataExample example = new DictionaryDataExample();
        DictionaryDataExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(DataEnum.INSERT_STATUS.getStatus());
        criteria.andDictValueEqualTo(dictValue);
        return dictionaryDataMapper.selectByExample(example);
    }

    @Override
    public DictionaryData dictionaryDataByDataValue(String dictDataValue) {
        DictionaryDataExample example = new DictionaryDataExample();
        DictionaryDataExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(DataEnum.INSERT_STATUS.getStatus());
        criteria.andDictdataValueEqualTo(dictDataValue);
        List<DictionaryData> dictionaryData = dictionaryDataMapper.selectByExample(example);
        return dictionaryData!=null?dictionaryData.get(0):null;
    }
}
